home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-07-24 | 3.3 KB | 136 lines | [TEXT/MPS ] |
- /*
- FixTables.cp
-
- An MPW Tool to fix CFront's virtual tables in order to
- have them initialized at run-time.
-
- by Patrick Beard.
-
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <Types.h>
-
- char line[512]; // a line of input.
- char temp[512]; // temporary buffer.
- char className[512]; // the current class name (mangled).
-
- void MIFixTables()
- {
- char *table_starts = "struct __mptr __vtbl__"; // begins each table declaration.
- int starts_match_len = strlen(table_starts);
- char *table_ends = "0,0,0};";
- int ends_match_len = strlen(table_ends);
- int index;
- int found_table = 0;
- char *token, *s, *d;
-
- // begin the function.
- fprintf(stdout, "void init_vtbls(void)\n{\n");
-
- while(fgets(line, sizeof(line), stdin)) {
- if(!found_table) {
- if(strncmp(table_starts, line, starts_match_len) == 0) {
- // we've started parsing a table.
- s = &line[starts_match_len]; d = className;
- while( *s != '[') *d++ = *s++;
- *d = 0;
- fprintf(stdout, "/* className = %s */\n", className);
- index = 1;
- found_table = 1;
- }
- } else {
- // we're processing a table.
- if(strncmp(table_ends, line, ends_match_len) != 0) {
- token = strtok(line, ",");
- if(strcmp(token, "0") != 0)
- fprintf(stdout, "__vtbl__%s[%d].d = %s;\n", className, index, token);
- token = strtok(NULL, ",");
- if(strcmp(token, "0") != 0)
- fprintf(stdout, "__vtbl__%s[%d].i = %s;\n", className, index, token);
- token = strtok(NULL, ","); // this token points to entry we want.
- fprintf(stdout, "__vtbl__%s[%d].f = %s;\n", className, index++, token);
- } else {
- fprintf(stdout, "__ptbl__%s = __vtbl__%s;\n", className, className);
- found_table = 0; // start over and search for another table.
- }
- }
- }
-
- // finish the function off.
- fprintf(stdout, "}\n");
- }
-
- void SIFixTables()
- {
- char *table_starts = "__vptp __vtbl__"; // begins each table declaration.
- int starts_match_len = strlen(table_starts);
- char *table_ends = "0};";
- int ends_match_len = strlen(table_ends);
- int index;
- int found_table = 0;
- char *token, *s, *d;
-
- // begin the function.
- fprintf(stdout, "void init_vtbls(void)\n{\n");
-
- while(fgets(line, sizeof(line), stdin)) {
- if(!found_table) {
- if(strncmp(table_starts, line, starts_match_len) == 0) {
- // we've started parsing a table.
- s = &line[starts_match_len]; d = className;
- while( *s != '[') *d++ = *s++;
- *d = 0;
- fprintf(stdout, "/* className = %s */\n", className);
- index = 1;
- found_table = 1;
- }
- } else {
- // we're processing a table.
- if(strncmp(table_ends, line, ends_match_len) != 0) {
- token = strtok(line, ",");
- fprintf(stdout, "__vtbl__%s[%d] = %s;\n", className, index++, token);
- } else {
- fprintf(stdout, "__ptbl__%s = __vtbl__%s;\n", className, className);
- found_table = 0; // start over and search for another table.
- }
- }
- }
-
- // finish the function off.
- fprintf(stdout, "}\n");
- }
-
-
- Boolean UsingMultipleInheritance(int argc, char** argv)
- {
- // by default, we chew on the tables assuming multiple inheritance.
- Boolean useMI = true;
- while(--argc) {
- char *arg = *++argv;
- if(arg[0] == '-') {
- switch(tolower(arg[1])) {
- case 'm':
- useMI = true;
- break;
- case 's':
- useMI = false;
- break;
- }
- }
- }
-
- return useMI;
- }
-
- int main(int argc, char** argv)
- {
- if(UsingMultipleInheritance(argc, argv))
- MIFixTables();
- else
- SIFixTables();
- return 0;
- }
-